//
//  ApplyViewController.m
//  OneCreditTreasure
//
//  Created by iOS Tedu on 16/8/29.
//
//

#import "ApplyViewController.h"
#import "ApplyInfoViewController.h"
#import "CheckCodeTableViewCell.h"
#import "CarInfoTableViewCell.h"

@interface ApplyViewController () <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, CheckCodeTableViewCellDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *commitBtn;

@property (nonatomic, strong) NSArray *dataArr;
//输入框
@property (nonatomic, strong) UITextField *nameTF;
@property (nonatomic, strong) UITextField *phoneNumTF;
@property (nonatomic, strong) UITextField *checkCodeTF;
@end

@implementation ApplyViewController
#pragma mark - CheckCodeTableViewCell delegate
- (void)getCheckCode:(UITableViewCell *)cell {
    [self.view showWarning:@"正在获取验证码,请耐心等待!"];
}

#pragma mark - UITableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 1) {
        CheckCodeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CheckCodeCell" forIndexPath:indexPath];
        cell.leftInfoLB.text = self.dataArr[indexPath.row];
        cell.inputTF.delegate = self;
        cell.inputTF.placeholder = @"请输入手机号";
        self.phoneNumTF = cell.inputTF;
        cell.delegate = self;
        return cell;
    } else {
        CarInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"inputCell" forIndexPath:indexPath];
        cell.leftLB.text = self.dataArr[indexPath.row];
        cell.infoTF.delegate = self;
        [cell.infoTF mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(kScreenW / 4 + 12 * AUTO_WIDTH);
            make.top.equalTo(4);
            make.bottom.equalTo(-4);
            make.right.equalTo(-8);
        }];
        
        if (indexPath.row == 0) {
            cell.infoTF.placeholder = @"请输入您的姓名";
            cell.infoTF.returnKeyType = UIReturnKeyNext;
            self.nameTF = cell.infoTF;
        } else {
            cell.infoTF.returnKeyType = UIReturnKeyDefault;
            cell.infoTF.placeholder = @"请输入验证码";
            self.checkCodeTF = cell.infoTF;
        }
        
        return cell;
    }
}

#pragma mark - UITableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - UITextField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == self.nameTF) {
        [self.phoneNumTF becomeFirstResponder];
    } else if (textField == self.phoneNumTF) {
        [self.checkCodeTF becomeFirstResponder];
    } else {
        [self.checkCodeTF resignFirstResponder];
    }
    return YES;
}

#pragma mark - 生命周期 life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self tableView];
}

- (instancetype)init {
    if (self = [super init]) {
        self.title = @"申请";
    }
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.nameTF resignFirstResponder];
    [self.phoneNumTF resignFirstResponder];
    [self.checkCodeTF resignFirstResponder];
}

#pragma mark - 方法 methods
- (void)commitInfo:sender {
    ApplyInfoViewController *vc = [ApplyInfoViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载 lazy loading
- (UITableView *)tableView {
    if(_tableView == nil) {
        _tableView = [[UITableView alloc] init];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(0);
            make.bottom.equalTo(self.commitBtn.mas_top).equalTo(0);
        }];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
        _tableView.backgroundColor = kRGBA(234, 234, 234, 0.8);
        
        [_tableView registerClass:[CheckCodeTableViewCell class] forCellReuseIdentifier:@"CheckCodeCell"];
        [_tableView registerClass:[CarInfoTableViewCell class] forCellReuseIdentifier:@"inputCell"];
        
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (UIButton *)commitBtn {
    if(_commitBtn == nil) {
        _commitBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.view addSubview:_commitBtn];
        [_commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(0);
            make.height.equalTo(44);
        }];
        [_commitBtn addTarget:self action:@selector(commitInfo:) forControlEvents:UIControlEventTouchUpInside];
        
        [_commitBtn setTitle:@"提交" forState:UIControlStateNormal];
        _commitBtn.backgroundColor = kRGBA(0, 0, 0, 0.8);
    }
    return _commitBtn;
}

- (NSArray *)dataArr {
    if(_dataArr == nil) {
        _dataArr = @[@"姓名",@"手机号",@"验证码"];
    }
    return _dataArr;
}

@end
